#
# \file
# \brief
# \author Marco Antonio Pinto Orellana
#
# -------------------------------------------------------------------------
#
# Copyright (C) 2012  (see AUTHORS file for a list of contributors)
# http://opentonitruum.sourceforge.net
#
# This file is part of OpenTonitruum - a simulator/library of signal trans-
# mission, modulation and processing in a wireless environment.
#
# OpenTonitruum is free software: you can redistribute it and/or modify it 
# under the terms of the Lesser GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at 
# your option) any later version.
#
# OpenTonitruum is distributed in the hope that it will be useful, but WITH-
# OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with OpenTonitruum.  If not, see <http://www.gnu.org/licenses/>.
#
# -------------------------------------------------------------------------
#

from __future__ import division
import os
import os.path
import sys

class FileDataSource(object):
	def __init__(self, filename, scale, frequency_sample):
		self.filename = filename
		self.datafile = open(filename, 'rt').readlines()
		self.frequency_sample = frequency_sample
		self.period_sample = 1/frequency_sample
		self.scale = scale
	
	def __iter__(self):
		time = 0
		for line in self.datafile:
			#yield Struct(time = time, value = line.strip())
			yield (time, float(line.strip()) * self.scale )
			time += self.period_sample

	@staticmethod
	def getFilesInDirectory(dirname):
		for root, dirs, files in os.walk(dirname, topdown = False):
			#print root, "--"
			#print dirs, "--"
			for file in files:
				ext = os.path.splitext(file)[1]
				if ext == '.txt':
					yield os.path.join(root, file)

	@staticmethod
	def getDefaultDirectory():
		dirname = os.path.join(os.path.dirname(sys.argv[0]), "Source")
		return (dirname if os.path.exists(dirname) else None)

class AutoDataSource(object):
	def __init__(self, frequency_sample):
		self.frequency_sample = frequency_sample
		self.period_sample = 1/frequency_sample
	
	def __iter__(self):
		time = 0
		for i in range(100):
			value = i * 12;
			yield (time, value * self.period_sample)
			time += self.period_sample

class DataSource(object):
	def __init__(self, isAuto = True, filename = '', scale = 0.0, frequency_sample = 0.1):
		if not isAuto:
			self.data = FileDataSource(filename, scale, frequency_sample)
		else:
			self.data = AutoDataSource(frequency_sample)
	
	def __iter__(self):
		return self.data.__iter__()
	
#f = FileDataSource(r"d:\OpenTonitruum\application\Source\SourceData001.txt", 1e-6, 255)
#print FileDataSource.getDefaultDirectory()
#for time, data in f:
#	print time, ": ", data
#FileDataSource.getFilesInDirectory(r"d:\OpenTonitruum\application\Source\\")
#FileDataSource.getFilesInDirectory(os.path.dirname(sys.argv[0]))

class SourceLayer(object):
	def __init__(self, isAuto = True, filename = '', scale = 0.0, frequency_sample = 0.1):
		self.datasource = DataSource(isAuto, filename, scale, frequency_sample)
	
	def __iter__(self):
		for time, value in self.datasource:
			#print value
			binvalue = [int(s) for s in bin(int(value*10000))[2:]]
			yield (time, binvalue)









#d:\OpenTonitruum\application\Source\SourceData001.txt